#include "Config.h"


Config::Config(void)
{
	conf["SoundLevel"] = "1.0";
	conf["LastSongId"] = "0";
	conf["LastSongProgress"] = "0.0";
	conf["WindowPositionX"] = "-1";
	conf["WindowPositionY"] = "-1";
	conf["Shuffle"] = "0";
	conf["Pause"] = "0";

	loadConfig(getConfigPath(L"Config.cfg"));
}


Config::~Config(void)
{
	saveConfig(getConfigPath(L"Config.cfg"));
}

void Config::loadConfig(wstring fileName)
{
	string name;
	string value;
	ifstream file(fileName.c_str(), ifstream::in);
	if (file)
	{
		while (!file.eof())
		{	
			file >> name;

			getline(file, value);


			// trim trailing spaces
			size_t endpos = value.find_last_not_of(" \t");
			if( string::npos != endpos )
			{
				value = value.substr( 0, endpos+1 );
			}


			// trim leading spaces
			size_t startpos = value.find_first_not_of(" \t");
			if( string::npos != startpos )
			{
				value = value.substr( startpos );
			}


			conf[name] = value;
			//cout << name << " " <<value << endl;
		}
	}
	file.close();
}
void Config::saveConfig(wstring fileName)
{
	ofstream file(fileName.c_str());
	map<string, string>::iterator curr,end;
    for( curr = conf.begin(), end = conf.end();  curr != end;  curr++ )
	{
		file << curr->first << " " << curr->second << endl;
	}
	file.close();
	
}

float Config::stringToFloat(string value)
{
	float f;
	if(EOF == sscanf(value.c_str(), "%f", &f))
	{
		//error
	}
	return f;
}

int Config::stringToInt(string value)
{
	int i;
	if(EOF == sscanf(value.c_str(), "%d", &i))
	{
		//error
	}
	return i;
}

string Config::IntToString(int value)
{
	string val;
	stringstream stream (stringstream::in | stringstream::out);
	stream << value;
	stream >> val;
	return val;
}

string Config::floatToString(float value)
{
	string val;
	stringstream stream (stringstream::in | stringstream::out);
	stream << value;
	stream >> val;
	return val;
}

int Config::getInt(string name)
{

	return stringToInt(conf[name]);
}
float Config::getFloat(string name)
{
	return stringToFloat(conf[name]);
}
string Config::getString(string name)
{
	return conf[name];
}

void Config::setInt(string name, int value)
{

	conf[name] = IntToString(value);
}
void Config::setFloat(string name , float value)
{
	conf[name] = floatToString(value);
}
void Config::setString(string name, string value)
{
	conf[name] = value;
}

void Config::print()
{
	map<string, string>::iterator curr,end;
	for( curr = conf.begin(), end = conf.end();  curr != end;  curr++ )
	{
		cout << curr->first << " " << curr->second << endl;
	}
}


void Config::setSoundLevel(float level)
{
	conf["SoundLevel"] = level;
}
float Config::getSoundLevel()
{
	return stringToFloat(conf["SoundLevel"]);
}

wstring Config::getConfigPath(wstring path)
{
	// Randa exe kelia
	WCHAR buffer[MAX_PATH];
	GetModuleFileNameW(NULL, buffer, sizeof(buffer));
	wstring exeDir = buffer;
	int place = exeDir.find_last_of('\\');
	exeDir = exeDir.substr(0, place);
	_wchdir(exeDir.c_str());



	return exeDir + L'\\' + path;



	//wstring filePath = exeDir;

	//string exeDir1(filePath.begin(), filePath.end());
	//exeDir1.assign(filePath.begin(), filePath.end());

	//// Confige nurodom naujo playlisto vieta
	//mConfig->setString("PlayListPath", exeDir1);
}

/*
void Config::setLastSong(int id)
{

}
int Config::getLastSong()
{
	return 0;
}
void Config::setPlaylist(string path)
{

}
string getPlaylist()
{
	return conf[""];
}
void Config::setLastSongProgress(float progress)
{

}

float Config::getLastSongProgress()
{
	return .0;
}
void Config::setShuffle(bool state)
{

}
bool Config::getShuffle()
{
	return false;
}
void Config::setPauze()
{

}
bool Config::getPause()
{
	return 0;
}
*/